In [ ]:
#!pip install geopy
#!pip install folium
#!pip install wordcloud
In [ ]:
import geopy
from geopy.geocoders import Nominatim
from geopy.geocoders import Nominatim
from geopy.geocoders import GoogleV3
from folium.plugins import HeatMap
from folium.map import FeatureGroup
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import folium
import pandas as pd
import numpy as np
import math
from geopy.geocoders import Nominatim
import folium
import matplotlib.pyplot as plt
from folium.plugins import HeatMap
from folium.plugins import MarkerCluster
import folium
import geopy.distance
from folium.plugins import HeatMapWithTime
%matplotlib inline 

Cordinates

In [ ]:
# Initialize the geolocator
geolocator = Nominatim(user_agent="your_user_agent")

# Add your location here
address = 'Christ University'  

# Geocode the address to get latitude and longitude
location = geolocator.geocode(address)

# Check if the location was found
if location is not None:
    # Extract latitude and longitude
    latitude = location.latitude
    longitude = location.longitude

    # Print the latitude and longitude
    print(f"Latitude: {latitude}")
    print(f"Longitude: {longitude}")

    # Reverse geocode to get the address
    location_reverse = geolocator.reverse((latitude, longitude))
    address_final = location_reverse.address
    print(f"Reverse Geocoded Address: {address_final}")
else:
    print("Location not found")
Latitude: 12.93396825
Longitude: 77.60635831895709
Reverse Geocoded Address: Christ University, 7th Cross Road, Sadgunte Palya, Suddagunte Palya Ward, South Zone, Bengaluru, Bangalore South, Bengaluru Urban District, Karnataka, 560029, India
Reverse Geocoded Address: Christ University, 7th Cross Road, Sadgunte Palya, Suddagunte Palya Ward, South Zone, Bengaluru, Bangalore South, Bengaluru Urban District, Karnataka, 560029, India

Haversine formula. distance in Kilometers given 2 geographical coordinates

In [ ]:
#Building a Formula
def distance(lat1, lon1, lat2, lon2):
  p = 0.017453292519943295 
  #p is for degree to radian ,π/180, used to ensure that the latitude and longitude values are correctly expressed in radians
  c = math.cos
  a = 0.5 - c((lat2 - lat1) * p)/2 + c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))/2
  return 12742 * math.asin(math.sqrt(a))

#bangalore lat1,lo1,lat2,lon2
d = distance(12.9703944526,77.6447132975,12.9732913,77.6404672)
print("The Distance is= ",d,'kilo meter')
The Distance is=  0.5616456785351014 kilo meter

Plotting Spatial Data on Maps

In [ ]:
#Creating the Map with latitudes and longitudes set to user location
user_map = folium.Map(location=[latitude, longitude], zoom_start=16)

# Plotting the user coordinates
a = folium.map.FeatureGroup()
location_name = address_final.split(',')[0]
a.add_child(folium.Marker([latitude, longitude], radius=10, color='black', fill_color='black', popup=location_name))
user_map.add_child(a)
user_map
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Plotting all the Restaurants in Bangalore using the dataset

In [ ]:
data = pd.read_csv("Data/Bangalore_Restaurants.csv")

#Plotting the data
#Itertive plot on the map
a = folium.map.FeatureGroup()

#Loop over the search dataset
for lat,lng,label in zip(data.Latitude,data.Longitude,data.Restaurant_Name):
  a.add_child(folium.CircleMarker([lat,lng],radius=2,color='red',fill=True,fill_color='black',popup=label))

#Checking the plot
user_map.add_child(a)
user_map
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
#heatmap
heatmap_data = data[['Latitude', 'Longitude']].values
user_map.add_child(HeatMap(heatmap_data, radius=15))
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook

MarkerCluster

In [ ]:
marker_cluster = MarkerCluster().add_to(user_map)
#  making markers
for lat, lng, label in zip(data.Latitude, data.Longitude, data.Restaurant_Name):
    folium.Marker(location=[lat, lng], popup=label).add_to(marker_cluster)   
user_map
#Zoom out to see the cluster, it will take some time
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
user_map = folium.Map(location=[latitude, longitude], zoom_start=16)

# user's location
user_location = (latitude, longitude)

# calculate distance from user location
def calculate_distance(lat, lon, location_name):
    location = (lat, lon)
    distance = geopy.distance.distance(user_location, location).km
    return f'Distance to {location_name}: {distance:.2f} km'

# making markers
for lat, lon, label in zip(data.Latitude, data.Longitude, data.Restaurant_Name):
    popup = folium.Popup(calculate_distance(lat, lon, label), max_width=200)
    folium.Marker(location=[lat, lon], popup=popup, tooltip=label).add_to(user_map)

user_map
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook